package org.jeecg.modules.questions;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.questions.entity.HistoryInfo;
import org.jeecg.common.modules.questions.entity.QuestionAll;
import org.jeecg.common.modules.questions.entity.QuestionHistory;
import org.jeecg.common.modules.questions.service.IHistoryInfoService;
import org.jeecg.common.modules.questions.service.IQuestionAllService;
import org.jeecg.common.modules.questions.service.IQuestionHistoryService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.UidUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 题库新
 * @Author: jeecg-boot
 * @Date: 2022-02-09
 * @Version: V1.0
 */
@Api(tags = "题库新")
@RestController
@RequestMapping("/questionAll/questionAll")
@Slf4j
public class QuestionAllController extends JeecgController<QuestionAll, IQuestionAllService> {
    @Autowired
    private IQuestionAllService questionAllService;

    /**
     * 分页模板列表查询
     *
     * @param questionAll
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "题库新-模板分页列表查询")
    @ApiOperation(value = "题库新-分页列表查询", notes = "题库新-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(QuestionAll questionAll,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<QuestionAll> queryWrapper = QueryGenerator.initQueryWrapper(questionAll, req.getParameterMap());
        queryWrapper.eq("template_type", "模板");
        Page<QuestionAll> page = new Page<QuestionAll>(pageNo, pageSize);
        IPage<QuestionAll> pageList = questionAllService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 分页模板列表查询
     *
     * @param questionAll
     * @param req
     * @return
     */
    @AutoLog(value = "题库新-模板分页列表查询")
    @ApiOperation(value = "题库新-分页列表查询", notes = "题库新-分页列表查询")
    @GetMapping(value = "/questionList")
    public Result<?> queryquestionList(QuestionAll questionAll, HttpServletRequest req) {
        List<QuestionVO> questionVOS = new ArrayList<>();
        List<QuestionAll> questionList = questionAllService.list(Wrappers.lambdaQuery(QuestionAll.class).eq(QuestionAll::getTemplateType, "问题").eq(QuestionAll::getTemplateId, questionAll.getTemplateId()).orderByAsc(QuestionAll::getQuestionNumber));
        for (QuestionAll question : questionList) {
            QuestionVO questionVO = new QuestionVO();
            BeanUtils.copyProperties(question, questionVO);

            List<QuestionAll> answerList = questionAllService.list(Wrappers.lambdaQuery(QuestionAll.class).eq(QuestionAll::getTemplateType, "答案").eq(QuestionAll::getTemplateId, questionAll.getTemplateId()).orderByAsc(QuestionAll::getQuestionNumber));
            if (!answerList.isEmpty() && answerList.size() > 0) {
                List<AnswerVO> answerVOs = new ArrayList<>();
                AnswerVO answerVO1 = new AnswerVO();
                for (QuestionAll answerVO : answerList) {
                    BeanUtils.copyProperties(answerVO, answerVO1);
                    answerVOs.add(answerVO1);
                }
                questionVO.setAnswerList(answerVOs);
            }
            questionVOS.add(questionVO);
        }
        return Result.ok(questionVOS);
    }

    /**
     * 添加
     *
     * @param questionAll
     * @return
     */
    @AutoLog(value = "题库新-添加")
    @ApiOperation(value = "题库新-添加", notes = "题库新-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody QuestionAll questionAll) {
        questionAllService.save(questionAll);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param questionAll
     * @return
     */
    @AutoLog(value = "题库新-编辑")
    @ApiOperation(value = "题库新-编辑", notes = "题库新-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody QuestionAll questionAll) {
        questionAllService.updateById(questionAll);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "题库新-通过id删除")
    @ApiOperation(value = "题库新-通过id删除", notes = "题库新-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        questionAllService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "题库新-批量删除")
    @ApiOperation(value = "题库新-批量删除", notes = "题库新-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.questionAllService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "题库新-通过id查询")
    @ApiOperation(value = "题库新-通过id查询", notes = "题库新-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        QuestionAll questionAll = questionAllService.getById(id);
        if (questionAll == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(questionAll);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param questionAll
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, QuestionAll questionAll) {
        return super.exportXls(request, questionAll, QuestionAll.class, "问卷模板");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcelQuestion(request, response, QuestionAll.class);
    }

    /**
     * 添加规则
     *
     * @return
     */
    @AutoLog(value = "题库新-添加规则")
    @ApiOperation(value = "题库新-添加规则", notes = "题库新-添加规则")
    @PostMapping(value = "/addRules")
    public Result<?> addRules(@RequestBody List<AnswerVO> answerVOS) {
        if (answerVOS == null) {
            return Result.error("未填写规则");
        }
        String telePlateId = answerVOS.get(0).getTemplateId();
        for (AnswerVO answerVO : answerVOS) {
            QuestionAll questionAll = new QuestionAll();
            if (answerVO.getAnswerNumber() == null) {
                questionAll = questionAllService.getOne(Wrappers.lambdaQuery(QuestionAll.class).eq(QuestionAll::getTemplateId, telePlateId).eq(QuestionAll::getQuestionNumber, answerVO.getQuestionNumber()));
                questionAll.setJumpQuestion(answerVO.getJumpQuestion());
            } else {
                questionAll = questionAllService.getOne(Wrappers.lambdaQuery(QuestionAll.class).eq(QuestionAll::getTemplateId, telePlateId).eq(QuestionAll::getParentId, answerVO.getParentId())
                        .eq(QuestionAll::getQuestionNumber, answerVO.getAnswerNumber().toUpperCase()));
                questionAll.setJumpQuestion(answerVO.getJumpQuestion());
            }
            questionAllService.updateById(questionAll);
        }
        return Result.ok("添加成功！");
    }

    /**
     * 问题列表查询
     *
     * @param questionAll
     * @param req
     * @return
     */
    @AutoLog(value = "题库新-问题列表查询")
    @ApiOperation(value = "题库新-问题列表查询", notes = "题库新-问题列表查询")
    @GetMapping(value = "/queryQList")
    public Result<?> queryQList(QuestionAll questionAll, HttpServletRequest req) {
        List<QuestionVO> questionVOS = new ArrayList<>();
        List<QuestionAll> questionList = questionAllService.list(Wrappers.lambdaQuery(QuestionAll.class).eq(QuestionAll::getTemplateType, "问题").eq(QuestionAll::getTemplateId, questionAll.getTemplateId()).last("  order by question_number+0 asc"));
        return Result.ok(questionList);
    }

    /**
     * 分页模板列表查询
     *
     * @param questionAll
     * @param req
     * @return
     */
    @AutoLog(value = "题库新-模板分页列表查询")
    @ApiOperation(value = "题库新-分页列表查询", notes = "题库新-分页列表查询")
    @GetMapping(value = "/queryAList")
    public Result<?> queryQorAList(QuestionAll questionAll, HttpServletRequest req) {
        List<QuestionVO> questionVOS = new ArrayList<>();
        List<QuestionAll> questionList = questionAllService.list(Wrappers.lambdaQuery(QuestionAll.class).eq(QuestionAll::getTemplateType, "答案").eq(QuestionAll::getTemplateId, questionAll.getTemplateId()).eq(QuestionAll::getParentId, questionAll.getParentId()).last("  order by question_number+0 asc"));
        return Result.ok(questionList);
    }
}
